package com.hhoss.logs;

import com.hhoss.lang.ClassMeta;
import org.slf4j.ILoggerFactory;

/**
 * logger handle util
 * @author kejun
 *
 */
public final class LoggerFactory {	
	/**
	 * @return Logger with caller's className as loggerName name
	 * should invoke method in endpoint class
	 */
	public static org.slf4j.Logger getLogger(){
		return getLogger(ClassMeta.callerName());
	}
	
	/**
	 * @return Logger with caller's className as loggerName name
	 */
	public static org.slf4j.Logger refLogger(){
		return getLogger(ClassMeta.invokerName());
	}
	
	/**
	 * @return Logger with given name
	 */
	public static org.slf4j.Logger getLogger(final String name){
		return org.slf4j.LoggerFactory.getLogger(name);
	}
	
	/**
	 * @return Logger with given name
	 */
	public static org.slf4j.Logger getLogger(final Class<?> cls){
		return org.slf4j.LoggerFactory.getLogger(cls);
	}
	
	public static ILoggerFactory getILoggerFactory() {
		return org.slf4j.LoggerFactory.getILoggerFactory();
	}	
	
	public static void setRootLevel(String level) {
		setLevel(Logger.ROOT_LOGGER_NAME,level);
	}	
	
	public static void setLevel(String name, String level) {
		org.slf4j.Logger log = LoggerFactory.getLogger(name);
		if(log instanceof ch.qos.logback.classic.Logger){
		  ((ch.qos.logback.classic.Logger)log).setLevel(ch.qos.logback.classic.Level.toLevel(level));
		}else{
			log.warn("wrong when set logger level[{}].",level);
		}
	}	

}
